#!/usr/bin/env python3
# coding: utf-8
"""
2024-02-19 
"""
from PySide6.QtWidgets import QTableWidget, QTableWidgetItem, QHeaderView, QAbstractItemView, QApplication, QWidget, QHBoxLayout, QVBoxLayout, QSizePolicy,QTableView
from PySide6.QtCore import QSize
from top.wardyan.core.vo.vos import Record, TableCfg, TableColCfg
## from top.wardyan.core.
from top.wardyan.core.utils.tableutil import *
## from top.wardyan.core.vo
## from top.wardyan.core.utils import winformutils
from top.wardyan.core.widges import tablemanager


from PySide6.QtGui import QStandardItemModel, QStandardItem,QColor,QBrush
from PySide6.QtCore import Qt, Signal, Slot
from PySide6.QtGui import QAction

from dateutil.parser import parse

from top.wardyan.core.widges.basetable import WBaseTableView





class WTableView(WBaseTableView) :

    ## _data = None

    def __init__(self, parent, widgetname: str, tablename: str, data: list[Record] = [], dbclick = None):
        super().__init__(parent, widgetname, tablename, data, dbclick=dbclick)
        ## self._data = data
        self.config()

    @Slot()
    def managecolumns(self) :
        win = tablemanager.TableEditorMain(self.widgetname, self.tablename)
        ## win.show()
        rs = win.exec()
        print(rs)
        


    def config(self) :
        self.setContextMenuPolicy(Qt.ContextMenuPolicy.ActionsContextMenu)

        self.initcolumnsetting = QAction('设置栏位')
        self.initcolumnsetting.triggered.connect(self.managecolumns)
        # self.paste = QAction('另存数据')
        # self.paste.triggered.connect(lambda: print('粘贴'))
        #self.addActions([self.initcolumnsetting])
        ## self.insertAction(self self.initcolumnsetting)
        self.addAction(self.initcolumnsetting)


    

# if __name__ == "__main__" :     ## or 1 == 1
#     app = QApplication([])
#     w = WWarningEditor()
#     ## w = WScriptWindow()
#     w.show()
#     app.exec()










##if __name__ == "__main__" :
    # app = QApplication([])
    
    # # 创建一个QWidget实例
    # w = QWidget()

    # # 设置窗口标题
    # w.setWindowTitle('My First QWidget')
    
    # # 设置窗口大小
    # w.setGeometry(100, 100, 1024, 768)
    # ## w.setContentsMargins(0, 0, 0, 0)

    # hb : QHBoxLayout = QHBoxLayout()
    # hb.setContentsMargins(0, 0, 0, 0)

    # w.setLayout(hb)
    # ## print(hb.geometry().size())
    # t : WTableWidget = WTableWidget(w, "__main__","maintable")
    # #print(x.geometry().size().height(), x.screen().availableGeometry().size().height())
    # ##t.setFixedHeight(x.geometry().size().height())
    
    # #t.showFullScreen()
    # ## t.setSizePolicy()
    # ## t.setSizeAdjustPolicy()
    # ## t.setSizePolicy(QSizePolicy.Policy.Expanding,QSizePolicy.Policy.Expanding)
    # ##b.addWidget(t)
    # t.setContentsMargins(0, 0, 0, 0)
    # t.setViewportMargins(0, 0, 0, 0)
    # hb.addWidget(t)
    
    # ##z : QHBoxLayout = QHBoxLayout()
    # # t.setSizePolicy(QSizePolicy.Policy.Expanding,QSizePolicy.Policy.Expanding)
    # # t.setLayout(z)

    # ## z.setContentsMargins(0, 0, 0, 0)
    

    # ## 最大化时要通知子控件也改变大小? 改变窗口大小时也要?
    # ## 还是要看怎么自动变

    
    # datas : list[Record] = []
    

    # r : Record = Record()
    # r.set("sID", 1)
    # r.set("sCode", "code1")
    # r.set("xName", "name1")
    # r.set("xGID", 1)
    # r.set("xDID", 1)
    # datas.append(r)

    
    # r : Record = Record()
    # r.set("sID", 6)
    # r.set("sCode", "code6")
    # r.set("xName", "name6")
    # r.set("xGID", 6)
    # r.set("xDID", 6)
    # datas.append(r)

    
    # r : Record = Record()
    # r.set("sID", 2)
    # r.set("sCode", "code2")
    # r.set("xName", "name2")
    # r.set("xGID", 2)
    # r.set("xDID", 2)
    # datas.append(r)

    
    # r : Record = Record()
    # r.set("sID", 3)
    # r.set("sCode", "code3")
    # r.set("xName", "name3")
    # r.set("xGID", 3)
    # r.set("xDID", 3)
    # datas.append(r)

    
    # r : Record = Record()
    # r.set("sID", 4)
    # r.set("sCode", "code4")
    # r.set("xName", "name4")
    # r.set("xGID", 4)
    # r.set("xDID", 4)
    # datas.append(r)

    
    # r : Record = Record()
    # r.set("sID", 5)
    # r.set("sCode", "code5")
    # r.set("xName", "name5")
    # r.set("xGID", 5)
    # r.set("xDID", 5)
    # datas.append(r)

    # t.render(datas)
    # w.show()
    # # t.layout()
    # ## w.showFullScreen()
    # #w.showMaximized()
    # app.exec()


    # app = QApplication([])
    
    # # 创建一个QWidget实例
    # w = QWidget()

    # # 设置窗口标题
    # w.setWindowTitle('My First QWidget')
    
    # # 设置窗口大小
    # w.setGeometry(100, 100, 1024, 768)

    # hb : QHBoxLayout = QHBoxLayout(w)
    # hb.setContentsMargins(0, 0, 0, 0)
    

    # ## t : WTableWidget = WTableWidget(w, "__main__","maintable")
    # t : WTableView = WTableView(w, "__main__","maintable")

    # hb.addWidget(t)
    

    # ## 最大化时要通知子控件也改变大小? 改变窗口大小时也要?
    # ## 还是要看怎么自动变

    
    # datas : list[Record] = []
    

    # r : Record = Record()
    # r.set("sID", 1)
    # r.set("sCode", "code1")
    # r.set("xName", "name1")
    # r.set("xGID", 1)
    # r.set("xDID", 1)
    # datas.append(r)

    
    # a : Record = Record()
    # a.set("sID", 6)
    # a.set("sCode", "code6")
    # a.set("xName", "name6")
    # a.set("xGID", 6)
    # a.set("xDID", 6)
    # datas.append(a)

    
    # b : Record = Record()
    # b.set("sID", 2)
    # b.set("sCode", "code2")
    # b.set("xName", "name2")
    # b.set("xGID", 2)
    # b.set("xDID", 2)
    # datas.append(b)

    
    # c : Record = Record()
    # c.set("sID", 3)
    # c.set("sCode", "code3")
    # c.set("xName", "name3")
    # c.set("xGID", 3)
    # c.set("xDID", 3)
    # datas.append(c)

    
    # d : Record = Record()
    # d.set("sID", 4)
    # d.set("sCode", "code4")
    # d.set("xName", "name4")
    # d.set("xGID", 4)
    # d.set("xDID", 4)
    # datas.append(d)

    
    # e : Record = Record()
    # e.set("sID", 5)
    # e.set("sCode", "code5")
    # e.set("xName", "name5")
    # e.set("xGID", 5)
    # e.set("xDID", 555)
    # datas.append(e)

    # t.render(datas, "")
    # w.show()
    # # t.layout()
    # ## w.showFullScreen()
    # #w.showMaximized()
    # app.exec()
















## 2024-06-09 F 禁用这玩意儿,暂时不再使用
# class WTableWidget (QTableWidget) :
#     ## def __init__(self, level=NOTSET) :
#     _data_ : list[Record] = []
#     widgetname : str = ""
#     tablename : str = ""
#     rh : int = 30
#     fields : list[str] = []

#     def __init__(self, parent, widgetname : str, tablename : str, data : list[Record] = []) :
#         QTableWidget.__init__(self, parent)
#         if data is None :
#             data = []
#         self.widgetname = widgetname
#         self.tablename = tablename
#         self.config()
#         self.render(data)
        
#     def config(self) :
#         """
#         从配置里读取
#         """
#         self.setSelectionBehavior(QAbstractItemView.SelectRows)
#         self.setSelectionMode(QAbstractItemView.SelectionMode.SingleSelection)
#         self.setEditTriggers(QTableWidget.EditTrigger.NoEditTriggers)
#         cfg : TableCfg = readTableCfg(self.widgetname, self.tablename)

        

#         hs = []
#         if cfg is not None :
#             self.rh = cfg.height
#             cols = cfg.cols
#             colcnt = 0
#             for c in cols :
#                 if c.show :
#                     colcnt += 1
#                     hs.append(c.title)
#             self.setColumnCount(colcnt)
#             idx : int = 0
#             for c in cols :
#                 if c.show :
#                     title : str = c.title
#                     field : str = c.field
#                     width : int = c.width
#                     item : QTableWidgetItem = QTableWidgetItem(title)
#                     item.field = field
#                     self.setColumnWidth(idx, width)
#                     self.setHorizontalHeaderItem(idx, item)
#                     self.fields.append(field)
#                     idx += 1
#         ## 设置列宽和对齐方式,格式化,替换值

#     # def buildColumns(self, data : list[Record]) -> None:
#     #     """
#     #     创建列定义
#     #     """
#     #     if self.model()

#     def render(self, data : list[Record], keyword : str = "") : 
#         if data is None :
#             data = []
#         self._data_ = data
#         rowcnt = len(data)
#         self.setRowCount(rowcnt)
#         ## colcnt = self.columnCount()
#         rowidx = 0 
#         for d in data :
#             i : int = 0
#             for field in self.fields :
#                 value = d.getStr(field)
#                 c : QTableWidgetItem = QTableWidgetItem(value)
#                 self.setItem(rowidx, i, c)
#                 i += 1
#             self.setRowHeight(rowidx, self.rh)
#             rowidx += 1
#         self.search(keyword=keyword)

#     def search(self, keyword : str = "") :
#         if keyword is None :
#             keyword = ""
#         if not keyword == "" :
#             for i in range(0, self.rowCount()) :
#                 finded : bool = False
#                 for j in range(0, self.columnCount()) :
#                     item : QTableWidgetItem = self.item(i, j) 
#                     t : str = item.text()
#                     if keyword in t :
#                         finded = True
#                         break
#                 if not finded :
#                     self.hideRow(i)
#                 else :
#                     self.showRow(i)
